<?php
/**
 * @abstract All the resources have to be one word
 * 
 */
class User_AdminController extends Clu_Controller_Action_Master
{
	public $m, $mRole; //model

	public function init()
	{
		$this->m = new user_Model_User();
		$this->mRole = new user_Model_Role();
		parent::init();
	}
	
	/*
    public function deleteAction()
    {
        $id = $this->_request->getParam('id');
        if ($id === null) 
        {
            $this->_forward('index');
            return;
        }
         
        $db = Zend_Db_Table::getDefaultAdapter() ;
        $db->beginTransaction();
        try {
            $sql = "DELETE FROM users WHERE id=".$db->quote($id, 'INTEGER') ;
            $db->query($sql);
            // TODO: delete from other places where user id is involved
            //
            //$perm_sql = "DELETE FROM permissions WHERE rid=".$db->quote($rid, 'INTEGER');
            //$db->query($perm_sql);
            $db->commit();
            
        }
        
        try
        {
        	$this->m->deleteByPrimary($id);
        }
        catch (Exception $e)
        {
            $db->rollBack();
            $this->renderToHolder('admin/error.phtml');
            die ($e->getMessage());
        }
        $this->renderToHolder('admin/delete.phtml');
    }
    */
    public function editAction()
    {
    	$sess = new Zend_Session_Namespace(CLU_NS);
    	//check user rights
       	if (strpos ($sess->userPermissions, 'administer everything') === false)
	    {
	    	$this->renderError(null,t(68));
            return;
	    }
    	//$id = $this->_request->getParam('id');
    }
    
    public function assignRolesAction()
    {
        $uid = $this->_request->getParam('uid');
        $uname = $this->_request->getParam('uname');
        
        $sess = new Zend_Session_Namespace(CLU_NS);
    	//check user rights
       	if (strpos ($sess->userPermissions, 'administer everything') === false)
	    {
	    	$this->renderError(null,t(68));
            return;
	    }
	    
        if ($uid === null) 
        {
            $this->_forward('index');
            return;
        } 
        //get permission list
        $this->viewParams['allRoles'] = $this->mRole->getAllPermissions();
        //current user roles  
        $userRoles = $this->m->getCurrentUserRoleIds($uid);
        $userRoleIds = array();
        
        if ($this->_request->isPost())
        {
            $params = $this->_request->getParams();
            foreach ($params as $k=>$v)
            {
                if (strpos($k, 'assigningRoles') === 0)
                {    
                    $userRoleIds[] = $v;
                }
            }
            // now insert user roles
            if ($this->m->insertUserRoles($uid, $userRoleIds))
	            $this->viewParams['saved'] = true;
	        else
	        {
	        	$this->renderError(null,t(504));
            	return;
	        }
        }
        else
        {
            foreach ($userRoles as $row)
                $userRoleIds[] = $row['rid'];
        }
        $this->viewParams['uid'] = $uid;
        $this->viewParams['uname'] = $uname;
        $this->viewParams['userRoleIds'] = $userRoleIds;
        
        //$this->renderToHolder('admin/assignroles.phtml');
        
    }
}
?>